<?php
//动态
namespace app\crmindex\controller;

use app\crm\model\Sn;
use app\crm\model\StaffAccessory;
use app\crm\model\SubregionManager;
use app\crm\model\SubregionSys;
use think\Db;
use think\Request;
use think\Exception;
use app\crm\model\Staff as StaffModel;
use weworkapi\api\datastructure\message\Message;
use weworkapi\api\datastructure\message\MpNewsArticle;
use weworkapi\api\datastructure\message\NewsArticle;
use weworkapi\api\datastructure\message\NewsMessageContent;
use weworkapi\api\datastructure\message\TextCardMessageContent;
use weworkapi\api\datastructure\message\TextMessageContent;
use weworkapi\api\datastructure\user\User;
use weworkapi\api\src\API;


class Dynamic extends \CrmindexController
{




    /**
     * 新增动态
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/save
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     * |subregion_id| int |1|社区id|
     *|content| var |1|发布内容|
     *|content_url| var |0|连接|
     *|title| var |0| 标题|
     *|abstract| var |0| 摘要|
     *|classify_id| int |0| 分类Id|
     *|is_share| int |0| 是否允许分享 0不允许 1允许|
     *|is_stick| int |0| 是否置顶 0否 1是|
     *|is_issue| int |0| 是否定时发布 0发布 1未发布|
     *|add_time| date |0| 定时发布时间|
     *|location| var |0| 定位地址|
     *|visible_type| var |0| 1公开、2对部分人可见、3对部分人不可见|
     *|line_staff[]| arr |0| 部分人可见 staff_id|
     *|online_staff[]| arr |0| 部分人不可见 staff_id|
     *|goods[[goods_id]]| arr |0| 商城商品 商品id|
     *|accessory[[accessory_url,accessory_name]]| arr |0| 附件[[附件地址，附件名]]|
     *|img[[img_url,img_name]]| arr |0| 图片[[图片地址，图片名]]|
     *
     */
    public function  save(Request $request){

        try {
            $param = delToken($request->param());

            $Dynamic = new \app\crmindex\model\Dynamic();
            $data = $Dynamic->add($param);
            if(!isset($data['dynamic_id'])){
                $this->successTerminate($data);
            }
            $this->t($data['dynamic_id'],$data['staff_id']);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    /**
     * 点赞动态 取消点赞
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/praise
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     *
     */
    public function praise(Request $request){
        try {
            $param =  delToken($request->param());
            $Dynamic = new \app\crmindex\model\Dynamic();
            $data = $data =$Dynamic->praise($param['dynamic_id']);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 评论动态
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/comment
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     *|pid| int |1| 回复评论评论id 没有为0|
     *|comment| var |1| 评论内容|
     */
    public function comment(Request $request){
        try {
            $param =  delToken($request->param());
            $Dynamic = new \app\crmindex\model\Dynamic();
            $data = $Dynamic->comment($param);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 获取评论动态
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/getComment
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|

     */
    public function getComment(Request $request){
        try {
            $param =  delToken($request->param());
            $Dynamic = new \app\crmindex\model\Dynamic();
            $data = $Dynamic->getComment($param);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 删除评论
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/commentDel
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|comment_id| int |1| 评论id|
     */
    public function commentDel(Request $request){
        try {
            $param =  delToken($request->param());
            $Dynamic = new \app\crmindex\model\Dynamic();
            $Dynamic->commentDel($param['comment_id']);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 删除动态
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/dynamicDel
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     */
    public function dynamicDel(Request $request){
        try {
            $param =  delToken($request->param());
            $Dynamic = new \app\crmindex\model\Dynamic();
            $Dynamic->dynamicDel($param['dynamic_id']);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 列表接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/listDynamic
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|subregion_id| int |1| 社区iD|
     *|classify_id| int |1| 分类id 全部为0|
     *|page| int |0| 页码 默认 1|
     *|pageSize| int |0| 单页数量 默认20|
     *|search| var |0| 搜索词|
     *|is_fs| int |0| 0已发送动态 1定时发送动态|
     *|type| int |0| 0全部动态，1上游动态，2下游动态，3包含附件动态，4自己发布动态|
     * 返回
     * dynamic_id	动态 id
        staff_id	"发布者id"
        add_time	"发布时间"
        content	"内容2"
        content_url	连接
        title	标题
        abstract	摘要
        classify_id	分类id
        is_share	是否允许分享 0不允许 1允许
        is_stick	是否置顶 0否 1是
        is_issue
        location	所在位置
        subregion_id	社区id
        staff_name	"发布者Name"
        head_img   发布者头像
        commentCount	评论条数
        praiseCount	点赞条数
        browseCount  阅读次数
        classify  [
            [
                classify_id  分类id
                classify_name 分类名
                表sh_crm_dynamic_classify
            ]
        ]
        accessory	[
            [
                accessory_url 附件地址
                accessory_name 附件name
                表sh_crm_dynamic_accessory
           ]
        ]
        goods	[
            商品数据 表sh_goods
       ]
        img	[
            动态图片 表sh_crm_dynamic_img
     ]
        comment[
            动态评论 sh_crm_dynamic_comment
        ]
    praise[
        点赞人员列表
    ]
     */
    public function listDynamic(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $subregion_id = $param['subregion_id'];
            $classify_id = $param['classify_id'];
            $data = $Dynamic->listDynamic($subregion_id,$classify_id,$param);
           // $this->successTerminate($data);
            $isend = 0;$text = '！';
            if(count($data)<1)
            {$isend = 1;$text = '没有更多数据了！';
            }
            json([
                'code' => 200,
                'message' => 'success',
                'data' => $data,
                 'isend'=>$isend,
                'text'=>$text
            ])->send();
            die();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 单条动态接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/infoDynamic
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态iD|

     * 返回
     * dynamic_id	动态 id
    staff_id	"发布者id"
    add_time	"发布时间"
    content	"内容2"
    content_url	连接
    title	标题
    abstract	摘要
    classify_id	分类id
    is_share	是否允许分享 0不允许 1允许
    is_stick	是否置顶 0否 1是
    is_issue
    location	所在位置
    subregion_id	社区id
    staff_name	"发布者Name"
    head_img   发布者头像
    commentCount	评论条数
    praiseCount	点赞条数
    browseCount  阅读次数
    classify  [
    [
    classify_id  分类id
    classify_name 分类名
    表sh_crm_dynamic_classify
    ]
    ]
    accessory	[
    [
    accessory_url 附件地址
    accessory_name 附件name
    表sh_crm_dynamic_accessory
    ]
    ]
    goods	[
    商品数据 表sh_goods
    ]
    img	[
    动态图片 表sh_crm_dynamic_img
    ]
    comment[
    动态评论 sh_crm_dynamic_comment
    ]
    praise[
    点赞人员列表
    ]
     */
    public function infoDynamic(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];
            $data = $Dynamic->infoDynamic($dynamic_id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 浏览接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/browse
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     */
    public function browse(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];
            $data = $Dynamic->browse($dynamic_id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 查询浏览记录接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/browseRecord
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     * |type| int |1| 0下游成员 1员工 2未浏览人数|
     * 返回
     *  dynamic_id 动态id
        staff_id	"人员id"
        is_downstream	1员工 0下游成员
        browse_time	"流浪时间
        id	1
        staff_name	"人员名"
        count	浏览次数
     */
    public function browseRecord(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];
            if(!isset($param['type'])||empty($param['type']))$param['type']=0;
            $type = $param['type'];
            $data = $Dynamic->browseRecord($dynamic_id,$type);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 查询未浏览人员列表接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/nobrowseRecord
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     * |type| int |1| 0下游成员 1员工 |
     * 返回
     *
        staff_id	"人员id"
        staff_name	"人员名"

     */
    public function nobrowseRecord(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];
            if(!isset($param['type'])||empty($param['type']))$param['type']=0;
            $type = $param['type'];
            $data = $Dynamic->getNobrowseRecordList($dynamic_id,$type);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 置顶取消置顶接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/stick
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     */
    public function stick(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];

            $data = $Dynamic->stick($dynamic_id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 置顶取消置顶接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/setClassify
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     *|classify_id[]| arr |1| 更改分类id|
     */
    public function setClassify(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $dynamic_id = $param['dynamic_id'];
            $classify_id = $param['classify_id'];
            $data = $Dynamic->setClassify($dynamic_id,$classify_id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 设置可见范围接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/setLink
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|dynamic_id| int |1| 动态id|
     *|visible_type| int |1| 1公开、2对部分人可见、3对部分人不可见|
     * |staff_id[]|arr|0|人员id|
     */
    public function setLink(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());

            $data = $Dynamic->setLink($param);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 置顶取消置顶接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/searchAccessory
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|search| var |0| 搜索词|
     返回
        dynamic_id	动态Id
        staff_id	"上传者Id"
        staff_name	"上传者"
        add_time	"上传时间"
        accessory_url	"文件url"
        accessory_name	"文件name"
     */
    public function searchAccessory(Request $request){
        try {
            $Dynamic = new \app\crmindex\model\Dynamic();
            $param =  delToken($request->param());
            $search = !empty( $param['search'])? $param['search']:'';

            $data = $Dynamic->searchAccessory($search);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 提醒浏览接口
     *接口地址 xlt.shared123.cn/crmindex/Dynamic/remindExamine
     *| 参数 | 参数类型 | 是否必须 | 备注 |
     *|staff_id| var |1| 人员id|
     *|dynamic_id| int |1| 动态ID|

     */
    public function  remindExamine(Request $request){
        try {
            $param =  delToken($request->param());
           // $DynamicModel = new \app\crmindex\model\Dynamic();
            $this->t($param['dynamic_id'],$param['staff_id']);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    public function t($dynamic_id,$staff_id){

        $message = new Message();
        {
            $message->sendToAll = false;
            $message->touser = $staff_id;
            $message->toparty = [];
            $message->totag= [];
            $message->agentid = 1000002;
            $message->safe = 0;
            $dynamic = Db::name('crm_dynamic')->where('dynamic_id',$dynamic_id)->find();

            $img_url = Db::name('crm_dynamic_img')->where('dynamic_id',$dynamic_id)->value('img_url');
           $dynamic_info_url =  Db::name('crm_sys')->where('id',1)->value('dynamic_info_url');

            $message->messageContent = new TextCardMessageContent(
                $title = $dynamic['title'],
                $description =  $dynamic['content'],
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa75fd57348cec1d7&redirect_uri=$dynamic_info_url/ZZTXWXBBS/index.w&state=dynamic_id$dynamic_id&response_type=code&scope=snsapi_base#wechat_redirect",
                $picurl = $img_url,
                $btntxt = "btntxt"
            );
        }

        $invalidUserIdList = null;
        $invalidPartyIdList = null;
        $invalidTagIdList = null;
        $this->qiyeweixin->MessageSend($message, $invalidUserIdList, $invalidPartyIdList, $invalidTagIdList);
    }
}